package com.example.lab2;

import java.text.DateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;
import database.DatabaseConnection;

public class RegisterActivity extends Activity {

	private RegisterActivity self = this;
	private DatabaseConnection datasource;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		final Spinner paymentSpinner = (Spinner) findViewById(R.id.payment_spinner);
		final Spinner expenseSpinner = (Spinner) findViewById(R.id.expense_spinner);

		datasource = new DatabaseConnection(this);
		datasource.open();

		Button saveButton = (Button) findViewById(R.id.save_button);
		saveButton.setOnClickListener(new Button.OnClickListener() {

			public void onClick(View v) {
				// get the current date
			//	String currentDateTimeString = DateFormat.getDateInstance()
				//		.format(new Date());

				EditText expenseText = (EditText) findViewById(R.id.expense_text_field);
				EditText dateText = (EditText) findViewById(R.id.date_text_field);
				EditText costText = (EditText) findViewById(R.id.amount_text_field);

				String expense = expenseText.getText().toString();
				String date = dateText.getText().toString();
				String cost = costText.getText().toString();
				String type = paymentSpinner.getSelectedItem().toString();
				String category = expenseSpinner.getSelectedItem().toString();
					// Print what we will save
					Toast.makeText(
							getApplicationContext(),
							"Saved " + expense + " for " + cost + " at " +
									date,
									Toast.LENGTH_SHORT).show();
					// saves the data
					datasource.addData(date, expense,
							category, type, cost);
				
			}

		});

		Button clearButton = (Button) findViewById(R.id.clear_button);
		clearButton.setOnClickListener(new Button.OnClickListener() {
			public void onClick(View v) {
		    	
				EditText exspenseTextField = (EditText)findViewById(R.id.expense_text_field);
				exspenseTextField.setText("");
				
				EditText costTextField = (EditText)findViewById(R.id.amount_text_field);
				costTextField.setText("");
				
				EditText dateField = (EditText)findViewById(R.id.date_text_field);
				dateField.setText("");
			}


			}
		);

		
		Button c = (Button)findViewById(R.id.graph_button);
		c.setOnClickListener(new Button.OnClickListener() {
			public void onClick (View view){
				GraphActivity pie = new GraphActivity();
				pie.setClothes(datasource.sumOfCosts("Clothes"));
		    	pie.setTransport(datasource.sumOfCosts("Transportation"));
		    	pie.setFood(datasource.sumOfCosts("Food"));
		    	pie.setHobby(datasource.sumOfCosts("Hobby"));
		    	pie.setIncome(datasource.expenseCalc("Income"));
		    	Intent lineIntent = pie.getIntent(self);
		        startActivity(lineIntent);
			}	
		});
		
		Button listButton = (Button) findViewById(R.id.list_button);
		listButton.setOnClickListener(new Button.OnClickListener() {
			public void onClick(View v) {
				
				
				ArrayList<String> myList = (ArrayList<String>) datasource.getAllData();
				Intent i = new Intent(self, ListActivity.class);
				i.putStringArrayListExtra("list", (ArrayList<String>) myList);
				startActivity(i);
			}
		});
		
		
		ArrayAdapter<CharSequence> paymentSpinnerArrayAdapter = ArrayAdapter
				.createFromResource(this, R.array.payment_spinner_array,
						android.R.layout.simple_list_item_1);

		paymentSpinnerArrayAdapter
				.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		paymentSpinner.setAdapter(paymentSpinnerArrayAdapter);
		paymentSpinner.setOnItemSelectedListener(new OnItemSelectedListener() {
			@Override
			public void onItemSelected(AdapterView<?> arg0, View arg1,
					int position, long id) {
				String ss = paymentSpinner.getSelectedItem().toString();
			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {
				// TODO Auto-generated method stub
			}
		});

		ArrayAdapter<CharSequence> expenseSpinnerArrayAdapter = ArrayAdapter
				.createFromResource(this, R.array.expense_spinner_array,
						android.R.layout.simple_list_item_1);

		expenseSpinnerArrayAdapter
				.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		expenseSpinner.setAdapter(expenseSpinnerArrayAdapter);
		expenseSpinner.setOnItemSelectedListener(new OnItemSelectedListener() {
			@Override
			public void onItemSelected(AdapterView<?> arg0, View arg1,
					int position, long id) {
				String ss = expenseSpinner.getSelectedItem().toString();
				
			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {
				// TODO Auto-generated method stub
			}
		});
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}
	

}
